<template>
  <div class="main">
    <!-- // submit表示提交表单数据时触发的事件，此处触发handleSubmit函数 -->
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      >
        <a-tab-pane key="tab1" tab="账号密码登录">
          <a-alert
            v-if="isLoginError"
            type="error"
            showIcon
            style="margin-bottom: 24px"
            message="账户或密码错误"
          />
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="帐户名："
              :allowClear="true"
              v-decorator="[
                'loginName', // 给表单赋值或拉取表单时，该input对应的key
                // @blur 是当元素失去焦点时所触发的事件
                {
                  rules: [{ required: true, message: '请输入帐户名' }],
                  validateTrigger: 'blur',
                },
              ]"
              autocomplete="off"
            >
              <a-icon
                slot="prefix"
                type="user"
                :style="{ color: 'rgba(0,0,0,.25)' }"
              />
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-password
              size="large"
              placeholder="密码: "
              :visibilityToggle="false"
              :allowClear="true"
              v-decorator="[
                'password',
                {
                  rules: [{ required: true, message: '请输入密码' }],
                  validateTrigger: 'blur',
                },
              ]"
            >
              <a-icon
                slot="prefix"
                type="lock"
                :style="{ color: 'rgba(0,0,0,.25)' }"
              />
            </a-input-password>
          </a-form-item>
        </a-tab-pane>
      </a-tabs>

      <!-- <a-form-item>
        <a-checkbox v-decorator="['rememberMe', { valuePropName: 'checked' }]">
          自动登录
        </a-checkbox>
        <router-link
          :to="{ name: 'recover', params: { user: 'aaa'} }"
          class="forge-password"
          style="float: right;"
        >忘记密码</router-link>
      </a-form-item> -->

      <a-form-item style="margin-top: 24px">
        <!-- size:大小 type:类型 htmltype:原生的type值（submit表示提交绑定的form表单） -->
        <a-button
        style="background:#fd79a8"
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >
          登 录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { timeFix } from "../../utils/util";

export default {
  data() {
    return {
      isLoginError: false,
      state: {
        time: 60,
        loginBtn: false,
        // login type: 0 email, 1 username, 2 telephone
        loginType: 0,
        smsSendBtn: false,
      },
      customActiveKey: "tab1",
      form: this.$form.createForm(this), // 只有这样注册后，才能通过表单拉取数据
    };
  },
  methods: {
    ...mapActions(["Login", "Logout"]),
    handleTabClick() {},
    handleSubmit(e) {
      e.preventDefault(); // 阻止冒泡 preventDefault()为阻止元素发生默认行为，此处为拦截表单数据提交
      const {
        form: { validateFields },
        state,
        // customActiveKey,
        Login,
      } = this;

      state.loginBtn = true;

      const validateFieldsKey = ["loginName", "password"];

      // 拉取表单数据的方法 通过validateFields的方法，能够校验必填项是否有值，若无，则页面会给出警告！
      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          // 这里做逻辑处理
          // console.log('login form', values)
          // const loginParams = { ...values }
          const loginParams = {
            username: values.loginName,
            password: values.password,
          };
          Login(loginParams)
            .then((res) => {
              if (res.data.code != 200) {
                setTimeout(() => {
                  this.$notification.error({
                    message: "登录失败",
                    description: res.data.message,
                  });
                }, 500);
              }
              this.loginSuccess(res);
            })
            .catch(err)
            .finally(() => {
              state.loginBtn = false;
            });
        } else {
          setTimeout(() => {
            state.loginBtn = false;
          }, 600);
        }
      });
    },
    // 登录成功提示
    loginSuccess(res) {
      console.log(res);
      this.$router.push({ path: "/" });
      // 正常登录提示
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: "欢迎",
          description: `${timeFix()}，欢迎回来`,
        });
      }, 500);
      this.isLoginError = false;
    },
  },
};
</script>

<style lang="less">
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
